package com.ronri.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/28.
 */

public abstract class CommonAdapter<T> extends BaseAdapter {

    // 需要显示字段名称
    private List<String> mFields = new ArrayList<>();

    // 视图
    protected LayoutInflater mInflater;

    // 句柄
    protected Context mContext;

    // 数据列表
    protected List<T> mDatas;

    protected List<Integer> mViewid;
    // 视图ID
    protected int mLayoutID;

    // 泛型类对象
    private Class<T> mClass = null;

    // 获取泛型对象
    @SuppressWarnings("unchecked")
    public Class<T> getClz(){
        if (mClass==null) {
            mClass = (Class<T>)(((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
        }
        return mClass;
    }

    /****
     * 构造函数
     * @param context
     * @param layoutId
     * @param mDatas
     */
    public CommonAdapter(Context context, int layoutId, List<T> mDatas) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
        this.mLayoutID = layoutId;

        // 获取泛型对象
        getClz();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /***
     * 获取布局文件,将控件初始化
     * @param convertView
     * @return
     */
    public Object initViewHolder(View convertView, int postion){
        try {
            Object e = mDatas.get(postion); //调用空构造函数

            if (convertView instanceof ViewGroup) {
                int viewCount = ((ViewGroup) convertView).getChildCount();

                for(int i = 0; i < viewCount; i++){
                    View view = ((ViewGroup) convertView).getChildAt(i);

                    String szId = mContext.getResources().getResourceEntryName(view.getId());

                    try {
                        Field field =  mClass.getDeclaredField(szId);

                        Object o = field.get(e);

                        // 文本
                        if (view instanceof TextView) {
                            String szValue = FormatData(szId, o.toString());
                            ((TextView) view).setText(szValue);
                        }

                        // 图片
                        if(view instanceof ImageView){

                        }
                    }catch (NoSuchFieldException ex){

                    }
                }
            }
            return e;
        } catch (IllegalAccessException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            return null;
        } catch (IllegalArgumentException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            return null;
        }
    }

    /****
     * 抽象方法,实现些方法用于数集的绑定
     * @param t
     * @param o
     */
    public abstract void addDataToView(T t,View o);

    /*****
     * 格式化字符串
     * @param szField
     * @param szValue
     * @return
     */
    public abstract String FormatData(String szField, String szValue);

    /****
     * 获取视图
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object e;
        if (convertView == null) {

            // 获取View视图
            convertView = mInflater.inflate(mLayoutID, null);

            // 初始化
            e = initViewHolder(convertView, position);

            if (e != null) convertView.setTag(e);
        }else{
            e = convertView.getTag();
        }
        addDataToView(mDatas.get(position), convertView);
        return convertView;
    }
}
